﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace HelloWPF.DependencyAttachedProperty
{
    /// <summary>
    /// PasswordBox的Password依赖附加属性
    /// </summary>
    public class DependenceAattachedPropertyPassword
    {
        public static string GetPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordProperty);
        }

        public static void SetPassword(DependencyObject obj, string value)
        {
            obj.SetValue(PasswordProperty, value);
        }

        /// <summary>
        /// 依赖附加属性
        /// 1.所属对象必须是依赖对象
        /// 2.必须是静态+Readonly
        /// 3.不是通过new，通过注册完成定义
        /// 4.约定俗称以Property结尾
        /// Password:CLR属性名
        /// typeof(string):CLR属性类型和依赖属性类型
        /// typeof(DependenceAattachedPropertyPassword):依赖属性所有者类型
        /// </summary>        
        public static readonly System.Windows.DependencyProperty PasswordProperty =
        System.Windows.DependencyProperty.RegisterAttached("Password", typeof(string), typeof(DependenceAattachedPropertyPassword), new PropertyMetadata(null, new PropertyChangedCallback((o, e) =>
        {
            var pwdObj = o as PasswordBox;
            pwdObj.Password = e.NewValue.ToString();
        })));
    }
}
